package cn.iocoder.yudao.module.ao.controller.admin.curriculum;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.curriculum.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.curriculum.CurriculumDO;
import cn.iocoder.yudao.module.ao.service.curriculum.CurriculumService;

@Tag(name = "管理后台 - 课程体系")
@RestController
@RequestMapping("/ao/curriculum")
@Validated
public class CurriculumController {

    @Resource
    private CurriculumService curriculumService;

    @PostMapping("/create")
    @Operation(summary = "创建课程体系")
    @PreAuthorize("@ss.hasPermission('ao:curriculum:create')")
    public CommonResult<Long> createCurriculum(@Valid @RequestBody CurriculumSaveReqVO createReqVO) {
        return success(curriculumService.createCurriculum(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程体系")
    @PreAuthorize("@ss.hasPermission('ao:curriculum:update')")
    public CommonResult<Boolean> updateCurriculum(@Valid @RequestBody CurriculumSaveReqVO updateReqVO) {
        curriculumService.updateCurriculum(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程体系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ao:curriculum:delete')")
    public CommonResult<Boolean> deleteCurriculum(@RequestParam("id") Long id) {
        curriculumService.deleteCurriculum(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程体系")
                @PreAuthorize("@ss.hasPermission('ao:curriculum:delete')")
    public CommonResult<Boolean> deleteCurriculumList(@RequestParam("ids") List<Long> ids) {
        curriculumService.deleteCurriculumListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程体系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:curriculum:query')")
    public CommonResult<CurriculumRespVO> getCurriculum(@RequestParam("id") Long id) {
        CurriculumDO curriculum = curriculumService.getCurriculum(id);
        return success(BeanUtils.toBean(curriculum, CurriculumRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程体系分页")
    @PreAuthorize("@ss.hasPermission('ao:curriculum:query')")
    public CommonResult<PageResult<CurriculumRespVO>> getCurriculumPage(@Valid CurriculumPageReqVO pageReqVO) {
        PageResult<CurriculumDO> pageResult = curriculumService.getCurriculumPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CurriculumRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程体系 Excel")
    @PreAuthorize("@ss.hasPermission('ao:curriculum:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCurriculumExcel(@Valid CurriculumPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CurriculumDO> list = curriculumService.getCurriculumPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程体系.xls", "数据", CurriculumRespVO.class,
                        BeanUtils.toBean(list, CurriculumRespVO.class));
    }

}